package com.suredy.app.project.ctrl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.project.model.Contract;
import com.suredy.app.project.model.ContractPayment;
import com.suredy.app.project.service.ContractPaymentSrv;

/**
 * 收款信息列表控制器
 * 
 * @author VIVID.G
 * @since 2015-7-30
 * @version v0.1
 */
@Controller
@RequestMapping("/payment/list")
public class PaymentListCtrl {

	@Autowired
	private ContractPaymentSrv paySrv;

	@RequestMapping(value = "/{contractId}")
	public String list(@PathVariable String contractId, Model model) {
		String view = "app/project/form-pay";

		if (StringUtils.isBlank(contractId))
			return view;

		ContractPayment search = new ContractPayment();
		search.setContract(new Contract());
		search.getContract().setId(contractId);

		List<ContractPayment> data = this.paySrv.readByEntity(search);

		// 模拟数据库对象
		Contract contract = search.getContract();

		model.addAttribute("mc", contract);

		if (data == null || data.isEmpty())
			return view;

		contract.setPays(data);

		for (ContractPayment p : data) {
			p.setContract(contract);
		}

		return view;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public boolean deleteCostForm(String[] id) {
		if (id == null || id.length <= 0)
			return false;

		return this.paySrv.deleteWithIds(id);
	}

	@RequestMapping(value = "/children-contract-pay-list/{contractId}", method = RequestMethod.GET)
	public String childrenContractPayList(@PathVariable String contractId, Model model) {
		String view = "app/project/child-contract-pay";

		if (StringUtils.isBlank(contractId)) {
			model.addAttribute("invalidContract", true);
			return view;
		}

		ContractPayment search = new ContractPayment();
		search.setContract(new Contract());
		search.getContract().setId(contractId);

		List<ContractPayment> data = this.paySrv.readByEntity(search);

		// 模拟数据库对象
		Contract contract = search.getContract();
		contract.setPays(data);

		model.addAttribute("data", contract);

		return view;
	}

}
